{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.qidian.com/all?page=3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "import requests\n",
    "import xlwt\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "book=xlwt.Workbook(encoding='utf-8')\n",
    "sheet=book.add_sheet('Sheet1')\n",
    "sheet.write(0,0,'python')\n",
    "sheet.write(1,1,'love')\n",
    "book.save('file/text.xls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_res(url):\n",
    "    headers={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}\n",
    "    response=requests.get(url,headers=headers)\n",
    "    return response.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_info():\n",
    "    format_url='https://www.qidian.com/all?page={}'\n",
    "    # 前10页\n",
    "    urls=[format_url.format(str(i)) for i in range(1,11)]\n",
    "    for url in urls:\n",
    "        selector=etree.HTML(get_res(url))\n",
    "        infos=selector.xpath('//ul[@class=\"all-img-list cf\"]/li')\n",
    "        for info in infos:\n",
    "            title=info.xpath('div[2]/h4/a/text()')[0]\n",
    "            author=info.xpath('div[2]/p[1]/a[1]/text()')[0]\n",
    "            style_1=info.xpath('div[2]/p[1]/a[2]/text()')[0]\n",
    "            style_2=info.xpath('div[2]/p[1]/a[3]/text()')[0]\n",
    "            style=style_1+'-'+style_2\n",
    "            complete=info.xpath('div[2]/p[1]/span/text()')[0]\n",
    "            introduce=info.xpath('div[2]/p[2]/text()')[0].strip()\n",
    "            info_list=[title,author,style,complete,introduce]\n",
    "            all_info_list.append(info_list)\n",
    "        time.sleep(1)\n",
    "        \n",
    "            #word=info.xpath('div[2]/p[3]/span/span/text()')[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_info_list = []\n",
    "get_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "book = xlwt.Workbook(encoding='utf-8') \n",
    "sheet= book.add_sheet('Sheetl')\n",
    "header= ['title','author','style','complete','introduce']  \n",
    "for h in range(len(header)):\n",
    "    sheet.write(0, h, header[h])\n",
    "i=1\n",
    "for list in all_info_list:\n",
    "    j=0\n",
    "    for data in list:\n",
    "        sheet.write(i, j, data)\n",
    "        j+=1\n",
    "    i+=1\n",
    "book.save('file/qidian.xls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
